﻿/*
 * Copyright (c) 2009, Thiago M. Sayao <thiago.sayao@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 *  * Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  * Neither the name of the author nor the names of its contributors may be 
 *    used to endorse or promote products derived from this software without 
 *    specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 */

using System.Web.Mvc;
using System.Web.Security;
using System.Text;
using System.Net.Mail;
using System;
using DotNetBurner.Model;
using DotNetBurner.Core;
using DotNetBurner.Web;
using DotNetBurner.Web.Settings;

namespace DotNetBurner
{
    public abstract class BaseController : Controller
    {
        protected BaseController()
        {
            Repository = new CachedRepository(SwissKnife.ConnectionString);
        }

        public Repository Repository
        { 
            get;
            set;
        }

        protected static MembershipProvider UserManager
        {
            get
            {
                return Membership.Provider;
            }
        }

        protected bool IsUserAuthenticated
        {
            get
            {
                return HttpContext.User.Identity.IsAuthenticated;
            }
        }

        protected string CurrentUserName
        {
            get
            {
                return IsUserAuthenticated ? HttpContext.User.Identity.Name : "Anonymous";
            }
        }

        protected MembershipUser CurrentUser
        {
            get
            {
                return !IsUserAuthenticated ? null : UserManager.GetUserCached(CurrentUserName, true);
            }
        }

        protected int CurrentUserId
        {
            get
            {
                if (!IsUserAuthenticated)
                    return 0;

                return CurrentUser == null ? 0 : Repository.GetUserId(CurrentUser.ProviderUserKey);
            }
        }

        protected bool IsUserAdm
        {
            get
            {
                if (!IsUserAuthenticated)
                    return false;

                return Roles.IsUserInRole(SiteSettings.Settings.AdminRole);
            }
        }

        protected bool IsUserModerator
        {
            get
            {
                if (!IsUserAuthenticated)
                    return false;

                return Roles.IsUserInRole(SiteSettings.Settings.AdminRole);
            }
        }

        protected bool IsUserAdmOrModerator
        {
            get
            {
                return (IsUserAdm || IsUserModerator);
            }
        }

        public T GetViewData<T>() where T : BaseViewData, new()
        {
            var viewData = new T
            {
                IsAuthenticated = IsUserAuthenticated,
                UserName = CurrentUserName,
                UserId = CurrentUserId,
                //Lazy is used here becase the categories user control will be entirely cached later
                Categories = new LazyCollection<Category>(() => Repository.GetCategories())
            };

            if(IsUserAuthenticated)
                viewData.User = CurrentUser;

            return viewData;
        }

        //TODO: Move to DotNetBurner.Web
        public static void SendMail(string to, string subject, string body)
        {

#if DEBUG
            return;
#endif

            using (var message = new MailMessage())
            {
                message.From = new MailAddress(SiteSettings.Settings.AdminMail);
                message.To.Add(new MailAddress(to));
                message.Subject = subject;
                message.Body = body;
                message.BodyEncoding = Encoding.UTF8;
                message.SubjectEncoding = Encoding.UTF8;

                var mailClient = new SmtpClient();
                mailClient.Send(message);
            }
        }
    }
}